package com.atung.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atung.dao.AdmSchoolDAO;
import com.atung.dao.BusCityStationDAO;
import com.atung.dao.BusProvinceCityDAO;
import com.atung.dao.BusRoutApplicationDAO;
import com.atung.dao.BusSchoolOpenRouteDAO;
import com.atung.dao.SchoolPageDAO;
import com.atung.dao.SchoolUserDAO;
import com.atung.model.AdmSchool;
import com.atung.model.BusCityStation;
import com.atung.model.BusProvinceCity;
import com.atung.model.BusRoutApplication;
import com.atung.model.BusSchoolOpenRoute;
import com.atung.model.BusSchoolUser;
import com.atung.response.ResponseChangeSchool;
import com.atung.response.ResponsePagePath;
import com.atung.service.SchoolPageService;

import cn.hutool.core.bean.BeanUtil;

@Service
public class SchoolPageServiceImpl implements SchoolPageService {

	@Autowired
	private SchoolPageDAO schoolPageDAO;
	@Autowired
	private SchoolUserDAO schoolUserDAO;
	@Autowired
	private AdmSchoolDAO admSchoolDAO;
	@Autowired
	private BusCityStationDAO busCityStationDAO;
	@Autowired
	private BusSchoolOpenRouteDAO busSchoolOpenRouteDAO;
	@Autowired
	private BusProvinceCityDAO busProvinceCityDAO;
	@Autowired
	private BusRoutApplicationDAO busRoutApplicationDAO;
	
	/**
	 * 获取学校信息
	 */
	@Override
	public ResponseChangeSchool selectSchoolNameByOpenId(String openId) throws Exception {
		// TODO 自动生成的方法存根
		ResponseChangeSchool responseChangeSchool=new ResponseChangeSchool();
		BusSchoolUser schoolUser=schoolUserDAO.selectUserAndOpenId(openId);
		if(Objects.isNull(schoolUser)){
			//用户不存在
			throw new Exception("USER IS NULL");
		}
		AdmSchool admSchool=admSchoolDAO.selectAdmSchoolById(schoolUser.getSchoolId());
		if(Objects.nonNull(admSchool)){
			BeanUtil.copyProperties(admSchool, responseChangeSchool);
			responseChangeSchool.setSchoolId(admSchool.getId());
		}

		return responseChangeSchool;
	}

	/**
	 * 切换学校
	 */
	@Override
	public List<ResponseChangeSchool> selectSchoolNameAll(String params) {
		// TODO 自动生成的方法存根
		List<ResponseChangeSchool> responseChangeSchoolList=new ArrayList<ResponseChangeSchool>();
		List<AdmSchool> admSchoolList=admSchoolDAO.selectSchoolNameLikeAll(params);
		if(!Objects.isNull(admSchoolList)){
			for(AdmSchool admSchool:admSchoolList){
				ResponseChangeSchool changeSchool=new ResponseChangeSchool();
				BeanUtil.copyProperties(admSchool, changeSchool);
				changeSchool.setSchoolId(admSchool.getId());
				responseChangeSchoolList.add(changeSchool);
			}
			
		}
		
		return responseChangeSchoolList;
	}

	@Override
	public List<ResponsePagePath>selectSchoolNameAndCity(String openId,Long schoolId, List<String> destination) throws Exception{
		List<ResponsePagePath> pagePathList=new ArrayList<>();
		//获取学校名字
		AdmSchool admSchool=admSchoolDAO.selectAdmSchoolById(schoolId);
		if(Objects.isNull(admSchool)){
			//学校编号不存在
			throw new Exception("ERR SCHOOLNUMBER IS NULL");
		}
		
		String province=destination.get(0);
		String city=destination.get(1);
		BusProvinceCity busProvinceCity=busProvinceCityDAO.selecyBusProvinceCityByProvinceAndCity(province.trim(),city.trim());
		
		if(Objects.isNull(busProvinceCity)){
			throw new Exception("ERR IS NULL");
		}
		Long cityId=busProvinceCity.getId();
		//获取全部汽车站
		List<BusCityStation> busCityStationList=busCityStationDAO.selectCityStationByCityId(cityId);
		if(busCityStationList.size()==0){
//			throw new Exception("你选择的城市暂无信息");
			return pagePathList;
		}
		for(BusCityStation busCityStation:busCityStationList){
			
			ResponsePagePath responsePagePath=new ResponsePagePath();
			//出发地城市id
			responsePagePath.setDepartCityId(admSchool.getSchoolCityId());
			//出发地id
			responsePagePath.setDepartlId(admSchool.getId());
			//出发地名字
			responsePagePath.setDepartlName(admSchool.getSchoolName());
			//目的城市id
			responsePagePath.setDestinationCityId(cityId);
			//目的地id
			responsePagePath.setDestinationId(busCityStation.getId());
			//目的地名字
			responsePagePath.setDestination(busCityStation.getStationName());
			responsePagePath.setIsDispark(1);
			BusRoutApplication busRoutApplication=busRoutApplicationDAO.selectRoutApplicationByOpenidAndDepartlIdAndDestinationId(openId, admSchool.getId(), busCityStation.getId());
			if(Objects.isNull(busRoutApplication)){
				responsePagePath.setIsApplication(0);
			}else{
				responsePagePath.setIsApplication(1);
			}
			pagePathList.add(responsePagePath);
		}
		
		List<BusSchoolOpenRoute> busCarRouteList=busSchoolOpenRouteDAO.selectCarRouteByCityIdAndschoolId(schoolId,cityId);
		for(ResponsePagePath responsePagePath:pagePathList){
			for(BusSchoolOpenRoute busSchoolOpenRoute:busCarRouteList){
				if(Objects.equals(responsePagePath.getDestinationId(),busSchoolOpenRoute.getDestinationId())){
					responsePagePath.setIsDispark(0);
					responsePagePath.setIsApplication(1);
					responsePagePath.setRoutId(busSchoolOpenRoute.getId());
				}
			}
		}
		return pagePathList;
	}

	@Override
	public List<ResponsePagePath> selectSchoolName(String schoolName) {
		// TODO 自动生成的方法存根
		return schoolPageDAO.selectRandom();
	}
	
	public List<ResponsePagePath>selectCityId(Long cityId){
		return schoolPageDAO.selectCityId(cityId);
	}

}
